﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WitsWebApi.DataContext.Configurations;
using WitsWebApi.Entity.EntityParam;

namespace WitsWebApi.DataContext
{
    public class MesDbContext : DbContext
    {
        public MesDbContext()
        {

        }
        protected IOptionsMonitor<DbConfiguration> DbOptions { get; }

        public MesDbContext(DbContextOptions<MesDbContext> options, IOptionsMonitor<DbConfiguration> dbOptions) : base(options)
        {
            DbOptions = dbOptions;
        }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
            // 针对访问不同的数据库，此处需要安装指定的 NuGet 包
            optionsBuilder.UseSqlServer(DbOptions.CurrentValue.SqlServerConnection, c => c.MigrationsAssembly("WitsWebApi"));

            //optionsBuilder.UseNpgsql(DbOptions.CurrentValue.SqlServerConnection, c => c.MigrationsAssembly("WitsWebApi"));
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            //Register all DbSets to DbContext
            var modelassembly = AppDomain.CurrentDomain.GetAssemblies().Where(x => !x.IsDynamic && x.FullName.Contains("WitsWebApi.Entity")).FirstOrDefault();
            foreach (Type type in modelassembly.ExportedTypes)
            {
                // 只对 表 实体进行 注入操作
                if (type.IsClass && type.Namespace == "WitsWebApi.Entity.Entities")
                {
                    var name = modelBuilder.GetType().GetMethods();
                    var method = modelBuilder.GetType().GetMethods().Where(x => x.Name == "Entity").FirstOrDefault();

                    if (method != null)
                    {
                        method = method.MakeGenericMethod(new Type[] { type });
                        method.Invoke(modelBuilder, null);
                    }
                }
            }

            ////Entity fields are mapped to the database and converted to lowercase
            //foreach (var entity in modelBuilder.Model.GetEntityTypes())
            //{
            //    // Replace table names 
            //    entity.SetTableName(entity.GetTableName().ToSnakeCase());

            //    // Replace column names    
            //    foreach (var property in entity.GetProperties())
            //    {
            //        property.SetColumnName(property.GetColumnName().ToSnakeCase());
            //    }

            //    // Replace key names    
            //    foreach (var key in entity.GetKeys())
            //    {
            //        key.SetName(key.GetName().ToSnakeCase());
            //    }
            //}

            //Load all IEntityTypeConfiguration from the current assembly
            modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly);
            //modelBuilder.BuildSoftDeleteEntities();
            base.OnModelCreating(modelBuilder);
        }
    }
}
